<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="模板名称"
            prop="name"
            :rules="[{ required: true, message: '模板名称不能为空', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="模板编码"
            prop="tempName"
            :rules="[{ required: true, message: '模板编码不能为空', trigger: 'blur' }]"
          >
            <el-input v-model="form.tempName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="可用平台"
            prop="pt"
            :rules="[{ required: true, message: '请选择可用平台', trigger: 'change' }]"
          >
            <el-checkbox-group v-model="zxpt" size="small" style="width: 400px">
              <el-checkbox v-for="item in kypt" :key="item.id" :border="true" :label="item.value">
                {{ item.label }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="所属大类"
            prop="ssdl.id"
            :rules="[{ required: true, message: '请选择所属大类', trigger: 'change' }]"
          >
            <el-select v-model="form.ssdl.id" clearable placeholder="请选择" @change="parentChange">
              <el-option
                v-for="item in parentList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="所属分类"
            prop="ssfl.id"
            :rules="[{ required: true, message: '请选择所属分类', trigger: 'change' }]"
          >
            <el-select v-model="form.ssfl.id" clearable placeholder="请选择">
              <el-option
                v-for="item in childList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="优先级"
            prop="px"
            :rules="[{ required: true, message: '排序值不能为空', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input-number v-model="form.px" :min="1" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="状态"
            prop="sfqy"
            :rules="[{ required: true, message: '请选分类状态', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.sfqy">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { queryAllChild, queryAllParent as queryFwfl } from '@/api/tp/fl/index'
import { saveObject, updateObject } from '@/api/tp/tpl/index'
export default {
  name: 'TplEntity',
  data() {
    return {
      loading: false,
      title: '模板管理',
      dialogFormVisible: false,
      form: {
        id: null,
        pt: '',
        tempName: '',
        name: '',
        ssdl: {
          id: null
        },
        ssfl: {
          id: null
        },
        sfqy: true,
        px: 1
      },
      backForm: {
        id: null,
        pt: '',
        tempName: '',
        name: '',
        ssdl: {
          id: null
        },
        ssfl: {
          id: null
        },
        sfqy: true,
        px: 1
      },
      zxpt: [],
      kypt: [
        { id: 1, value: 'pc', label: 'PC端' },
        { id: 2, value: 'app', label: '移动端' }
      ],
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }],
      parentList: [],
      childList: []
    }
  },
  watch: {
    zxpt: {
      handler(newVal, oldVal) {
        this.form.pt = newVal.toString()
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: true // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    this.initAllParent()
  },
  methods: {
    openEntity(data) {
      this.zxpt = []
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = data
        if (undefined !== data.pt && data.pt !== null) {
          this.zxpt = data.pt.split(',')
        }
        this.initChild()
      } else {
        this.form = Object.assign({}, this.backForm)
      }
      this.dialogFormVisible = true
    },
    close() {
      this.$refs['form'].resetFields()
      this.$emit('fetch-data')
      this.dialogFormVisible = false
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.$message.error('更新失败！')
              this.loading = false
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.loading = false
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    initAllParent() {
      queryFwfl().then(res => {
        const { data } = res
        this.parentList = data
      }).catch(() => {
        console.log('数据初始化失败！')
      })
    },
    initChild() {
      if (undefined !== this.form.ssdl && undefined !== this.form.ssdl.id && this.form.ssdl.id !== null) {
        queryAllChild({ pid: this.form.ssdl.id }).then(res => {
          const { data } = res
          this.childList = data
        }).catch(() => {
          console.log('数据初始化失败！')
        })
      }
    },
    parentChange(data) {
      this.form.ssfl.id = null
      this.childList = []
      this.initChild()
    }
  }
}
</script>

<style scoped>

</style>
